package api

import (
	"github.com/gin-gonic/gin"
	"vtrip/global"
	"vtrip/internal/service"
	"vtrip/pkg/app"
	"vtrip/pkg/errcode"
)

type ArticleComment struct{}

func NewArticleComment() ArticleComment {
	return ArticleComment{}
}

// All
// @Summary 获取某文章的所有评论
// @Produce json
// @Param token header string true "Token"
// @Param article_id body number true "文章 id"
// @Param page body number false "页码"
// @Param page_size body number false "页大小"
// @Router /article/comment/all [post]
// @Success 200 {object} []model.CommentPart "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
func (ac ArticleComment) All(c *gin.Context) {
	// 参数校验
	param := service.GetAllCommentRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		errResponse := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errResponse)
		return
	}

	// 业务处理
	svc := service.New(c.Request.Context())
	comments, err := svc.GetAllComments(&param)
	if err != nil {
		global.Logger.Errorf("svc.GetAllComments err: %v", err)
		response.ToErrorResponse(errcode.GetAllArticleFail)
		return
	}

	response.ToSuccessResponse(gin.H{
		"comments": comments,
	})
}

// Get
// @Summary 查看某评论的全部回复
// @Produce json
// @Param token header string true "Token"
// @Param comment_id body number true "评论 id"
// @Router /article/comment/get [post]
// @Success 200 {object} []model.CommentPart "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
func (ac ArticleComment) Get(c *gin.Context) {
	// 参数校验
	param := service.GetCommentReplyRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		errResponse := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errResponse)
		return
	}

	// 业务处理
	svc := service.New(c.Request.Context())
	comments, err := svc.GetCommentReply(&param)
	if err != nil {
		global.Logger.Errorf("svc.GetCommentReply err: %v", err)
		response.ToErrorResponse(errcode.GetAllArticleFail)
		return
	}

	response.ToSuccessResponse(gin.H{
		"comments": comments,
	})
}

// Make
// @Summary 发表评论
// @Produce json
// @Param token header string true "Token"
// @Param article_id body number true "文章 id"
// @Param content body string true "评论内容"
// @Router /article/comment/make [post]
// @Success 200 {object} nil "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
func (ac ArticleComment) Make(c *gin.Context) {
	// 参数校验
	param := service.MakeCommentRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		errResponse := errcode.InvalidParams.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errResponse)
		return
	}

	// 业务处理
	svc := service.New(c.Request.Context())
	userId := c.GetInt("user_id")
	err := svc.MakeArticle(userId, &param)
	if err != nil {
		global.Logger.Errorf("svc.MakeArticle err: %v", err)
		response.ToErrorResponse(errcode.PublishArticleFail)
		return
	}

	response.ToSuccessResponse(nil)
}

// Reply
// @Summary TODO 回复某评论
// @Produce json
// @Param token header string true "Token"
// @Param comment_id body number true "要回复的评论 id"
// @Param content body string true "回复内容"
// @Router /article/comment/reply [post]
// @Success 200 {object} nil "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
func (ac ArticleComment) Reply(c *gin.Context) {}

// Like
// @Summary TODO 为某评论点赞
// @Produce json
// @Param token header string true "Token"
// @Param comment_id body number true "要点赞的评论 id"
// @Router /article/comment/like [post]
// @Success 200 {object} nil "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
func (ac ArticleComment) Like(c *gin.Context) {}

// Dislike
// @Summary TODO 为某评论取消点赞
// @Produce json
// @Param token header string true "Token"
// @Param comment_id body number true "要取消点赞的评论 id"
// @Router /article/comment/dislike [post]
// @Success 200 {object} nil "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
func (ac ArticleComment) Dislike(c *gin.Context) {}
